<template>
  <div class="body">
    <div class="box">
      <div class="head">
        <img src="/static/daySignIn.png" />
      </div>
      <div class="middle">
        <div class="gold" :class="{waitCheck:item.wait}" v-for="(item,index) in goldList" :key="index" @click="checkDay(item,index)">
          <div class="mask" v-if="item.state">已签到</div>
          <img src="/static/signGold.png" />
          <div class="gold-num" v-if="!item.state">{{item.value}}</div>
        </div>
      </div>
      <div class="btn" @click="checkDay">签到</div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      goldList: [
        { state: false, value: 20, wait: false },
        { state: false, value: 40, wait: false },
        { state: false, value: 60, wait: false },
        { state: false, value: 80, wait: false },
        { state: false, value: 100, wait: false },
        { state: false, value: 120, wait: false },
        { state: false, value: 150, wait: false }
      ]
    };
  },
  computed: {
    ...mapState(["initInfo"])
  },
  methods: {
    formatGoldList() {
      console.log("序列化列表");
      let list = this.goldList;
      if (this.initInfo.signDay == 0) {
        return;
      } else {
        list.forEach((dayItem, index) => {
          if (index < this.initInfo.signDay) {
            dayItem.state = true;
          }
        });
        for (let i = 0; i < list.lengtn; i++) {
          if (!item.state) {
            item.wait = true;
            break;
          }
        }
        // list.forEach((item,index)=>{
        //   if(!item.state){
        //     item.wait=true;
        //     // break;
        //   }
        // })
        this.goldList = list;
      }
    },
    checkDay(item, index) {
        if(item.state){
          wx.showToast({
            title:"不能重复签到",
            icon:"none"
          })
          return;
        }

        // 点击 天数 签到
        let checkDayList = {};
        // let localDayList = wx.getStorageSync("checkDayList"); //同步获取
        // 点击 非当天
        if (index != undefined && index > this.initInfo.signDay) {
          if (this.initInfo.isSigned==1) {
            wx.showToast({
              title: "已经签到过了",
              icon: "none"
            });
          } else {
            wx.showToast({
              title: "前面还没有签到呢~",
              icon: "none"
            });
          }
          return false;
        }


        // 点击 当天
        if (index != undefined && index == this.initInfo.signDay) {
          this.goldList[index]["state"] = true;
          //本地存储 签到列表
          // checkDayList[this.initInfo.signDay] = true;
          // checkDayList["date"] = new Date();
          // wx.setStorage({
          //   key: "checkDayList",
          //   data: checkDayList
          // });
          // 签到成功
          this.checkOk();

          return false;
        }
        // 点击按钮签到
        if (this.initInfo.isSigned==0) {
          this.goldList[this.initInfo.signDay]["state"] = true;
          // checkDayList[this.initInfo.signDay] = true;
          // checkDayList["date"] = new Date();
          // wx.setStorage({
          //   key: "checkDayList",
          //   data: checkDayList
          // });
          // 签到成功
          this.checkOk();
        } else {
          wx.showToast({
            title: "已经签到过了",
            icon: "none",
            duration: 1000
          });
        }
    },
    checkOk() {
      this.fly
        .post("/signIn", {
          signDay: this.initInfo.signDay + 1,
          money: this.goldList[this.initInfo.signDay].value
        })
        .then(res => {
          console.log(res);
          wx.showToast({
            title: "签到成功",
            icon: "none",
            duration: 1000
          });
          setTimeout(() => {
            this.$emit("close");
          }, 1000);
        });
    }
  },
  mounted() {
    this.formatGoldList();
  }
};
</script>
<style lang="scss" scoped>
.body {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 50;
  background: rgba(#000000, 0.5);
}
.box {
  width: 630rpx;
  height: 713rpx;
  //border: 1px solid #ccc;
  position: absolute;
  top: 162rpx;
  left: 50%;
  transform: translateX(-50%);
  background-color: white;
  border-radius: 10rpx;
  .head {
    margin-bottom: 46rpx;
    img {
      width: 100%;
      height: 185rpx;
    }
  }
  .middle {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    .gold {
      width: 114rpx;
      height: 130rpx;
      margin-right: 24rpx;
      margin-bottom: 34rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #eeeeee;
      border-radius: 10rpx;
      position: relative;
      box-sizing: border-box;
      &:nth-of-type(4) {
        margin-right: 0;
      }
      &:nth-of-type(5),
      &:nth-of-type(6),
      &:nth-of-type(7) {
        margin-bottom: 0;
      }
      img {
        width: 72rpx;
        height: 71rpx;
        position: fixed;
      }
      .gold-num {
        z-index: 10;
        margin-top: 64rpx;
        margin-left: 60rpx;
        color: #9b9b9b;
        font-size: 30rpx;
      }
      .mask {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 30rpx;
        color: #fff;
        background: rgba(#000, 0.4);
        z-index: 5;
        border-radius: 10rpx;
      }
    }
    .wait-check {
      border: 4rpx solid #e55f80;
    }
  }
  .btn {
    width: 420rpx;
    height: 90rpx;
    font-size: 30rpx;
    color: #fff;
    background: #e55f80;
    border-radius: 1000rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 48rpx auto 0 auto;
  }
}
</style>


